@echo off
:: =================================
::	DownloadOfficecmd.bat 2024-11-03
::
::	DownloadOffice2021.zip
::	DownloadOffice.cmd		DownloadOffice.ico		DownloadOffice2021.bat		DownloadOffice2021.ini
::
::	EndCmd
:: =================================

set "para_n=Office 2021"

::	ParaIniType
::	IniFolder	ShortcutFolder
::	EndCmd

::	p1	p2	p3

::	Driver	FilePath
::	count

::	Input_Msg=
::	Input_ToRun=
::	IsToRun=0
::	AutoToRun=0

rem ====----	ini/ini_Ghost/ini_USBFormat
if "%~1"=="" (
	set "IniFolder=ini"
	set "ShortcutFolder=Shortcut"
) else (
	set "IniFolder=%~1"
	set "ShortcutFolder=%~1"
)
rem	====----誤用 else 會造成空白
if /i "%~1"=="ini" (
	set "ShortcutFolder=Shortcut"
)

rem ====----	all/del/curl/desktop	/lnk
if "%~2"=="" (
	set "ParaIniType=all"
) else (
	set "ParaIniType=%~2"
)

rem ====----	EndCmd	1 not 3
if "%~1"=="" (
	set "EndCmd=k"
) else (
	set "EndCmd=c"
)

rem ====----	AutoToRun
if "%~4"=="" (
	set /a AutoToRun=0
	set /a IsToRun=0
) else (
	set /a AutoToRun=1
	set /a IsToRun=1
)

rem ====	基本系統設定 set 一定要加""避免空白
set "Driver=%~d0"
set "FilePath=%~p0"
set "ConfFile=%~n0"
set "FullPath=%~dp0"
set "FullPathFile=%~f0"

title %~nx0

rem ====----	IsAdmin
call :IsAdmin

cd /D "%Driver%%FilePath%"
rem ==--

set chcp_Code=
For /f "tokens=2 delims=:" %%G in ('chcp') do set chcp_Code=%%G

rem ====----	Set Color
call :ColorSet

rem ====----	ToRunSetup
set para_n=%para_n% %~n0%~x0 %~2

if %AutoToRun% EQU 0 (
	call :ToRunSetup
)

rem ====----	IsToRun
if %IsToRun% EQU 0 (
	goto End
)

rem ====----	Checkdir
call :Checkdir %PUBLIC%\desktop\PortableApps\
call :Checkdir %PUBLIC%\desktop\OfficeProPlus2021\

rem ====----	7z_ShortCut
if not exist %SystemDrive%\PortableApps\Shortcut\Shortcut.exe (
	call :InstIni 7z_ShortCut %ParaIniType% %EndCmd%
	call :InstLnk 7z_ShortCut %ParaIniType% %EndCmd%
	rem echo Not found %SystemDrive%\PortableApps\Shortcut\Shortcut.exe
)

rem ====	ini	Folder	Framework35	lsl_Portable	Office	Shell
::	all
if /i "%ParaIniType%"=="all" (
	call :InstIni %IniFolder% %ParaIniType% %EndCmd%
	call :InstLnk %ShortcutFolder% %ParaIniType% %EndCmd%
	goto End
)

::	del
if /i "%ParaIniType%"=="del" (
	call :InstIni %IniFolder% %ParaIniType% %EndCmd%
	call :InstLnk %ShortcutFolder% %ParaIniType% %EndCmd%
	goto End
)

::	curl
if /i "%ParaIniType%"=="curl" (
	call :InstIni %IniFolder% %ParaIniType%
	goto End
)

::	Desktop
if /i "%ParaIniType%"=="Desktop" (
	call :InstIni %IniFolder%
	goto End
)

rem ====	Shortcut	Folder
::	lnk	bat
if /i "%ParaIniType%"=="lnk" (
	call :InstLnk %ShortcutFolder% %ParaIniType% %EndCmd%
	goto End
)

goto End

:InstIni
	rem ====	parameter all/del/curl/desktop
	set p1=%~1
	set p2=%~2
	set p3=%~3
rem	set p3=c

	setlocal enabledelayedexpansion
	echo.
	set count=0
	For /f "tokens=*" %%I in ('dir %p1%\*.ini /b') do (
		set /a count+=1
		echo !count!	%ClolorYellow_BT%Download.cmd %ClolorGreen_BT%"%p1%\%%I" %ClolorYellow_BT% %p2%%DefaultClolor%
		echo.
		call Download.cmd "%p1%\%%I"  %p2%  %p3%
	)
	setlocal
	exit /b

:InstLnk
	rem ====	p2 lnk	p3 parameter k or c
	set p1=%~1
	set p2=%~2
	set p3=%~3
	set p3=c

	setlocal enabledelayedexpansion
	echo.
	set count=0
	For /f "tokens=*" %%I in ('dir %p1%\*.bat /b') do (
		set /a count+=1
		echo !count!	%ClolorYellow_BT%%p1%\%%I %p2%%DefaultClolor%
		call "%p1%\%%I" %p3%
		echo.
	)
	setlocal
	exit /b

:: ================================================
:InstallAll
	setlocal enabledelayedexpansion
	echo.
	set count=0
	For /f "tokens=*" %%I in ('dir %IniFolder%\*.ini /b') do (
		set /a count+=1
		echo !count!	%ClolorYellow_BT%Download.cmd %ClolorGreen_BT%"%IniFolder%\%%I" %ClolorYellow_BT% all %DefaultClolor%
		echo.
		call Download.cmd "%IniFolder%\%%I" all
	)
	setlocal
	exit /b

:InstallDel
	setlocal enabledelayedexpansion
	echo.
	set count=0
	For /f "tokens=*" %%I in ('dir %IniFolder%\*.ini /b') do (
		set /a count+=1
		echo !count!	%ClolorYellow_BT%Download.cmd %ClolorGreen_BT%"%IniFolder%\%%I" %ClolorYellow_BT% del %DefaultClolor%
		echo.
		call Download.cmd "%IniFolder%\%%I" del
	)
	setlocal
	exit /b

:InstallCurl
	setlocal enabledelayedexpansion
	echo.
	set count=0
	For /f "tokens=*" %%I in ('dir %IniFolder%\*.ini /b') do (
		set /a count+=1
		echo !count!	%ClolorYellow_BT%Download.cmd %ClolorGreen_BT%"%IniFolder%\%%I" %ClolorYellow_BT% curl %DefaultClolor%
		echo.
		call Download.cmd "%IniFolder%\%%I" curl
	)
	setlocal
	exit /b

:InstallDesktop
	setlocal enabledelayedexpansion
	echo.
	set count=0
	For /f "tokens=*" %%I in ('dir %IniFolder%\*.ini /b') do (
		set /a count+=1
		echo !count!	%ClolorYellow_BT%Download.cmd %ClolorGreen_BT%"%IniFolder%\%%I" %ClolorYellow_BT% Desktop %DefaultClolor%
		echo.
		call Download.cmd "%IniFolder%\%%I"
	)
	setlocal
	exit /b

:InstallLnk
	setlocal enabledelayedexpansion
	echo.
	set count=0
	For /f "tokens=*" %%I in ('dir %ShortcutFolder%\*.bat /b') do (
		set /a count+=1
		echo !count!	%ClolorYellow_BT%%ShortcutFolder%\%%I%DefaultClolor%
		call "%ShortcutFolder%\%%I" c
		echo.
	)
	setlocal
	exit /b

:: ================================================

:ToRunSetup
	set Input_Msg=
	set Input_ToRun=
	set /a IsToRun=0

	rem echo %chcp_Code%
	if /i %chcp_Code% EQU 950 (
		set Input_Msg=%ClolorMagenta_NB%	你要安裝 %para_n% 應用程式嗎 ? 輸入 yes 或 no (y/n^):%DefaultClolor% %DefaultClolor%
	) else (
		set Input_Msg=%ClolorMagenta_NB%	Do you want to Install %para_n% Programs? Enter yes or no (y/n^):%DefaultClolor% %DefaultClolor%
	)

	set /p Input_ToRun=%Input_Msg%
	if not '%Input_ToRun%'=='' set Input_ToRun=%Input_ToRun:~0,1%

	if /i '%Input_ToRun%'=='y' (
		set /a IsToRun+=1
	) else (
		set /a IsToRun=0
	)

	if '%Input_ToRun%'=='' (
		set /a IsToRun=0
	)
	exit /b

:ColorSet
	::	1 Normal Text
	set "ClolorBlack_NT=[30m"
	set "ClolorRed_NT=[31m"
	set "ClolorGreen_NT=[32m"
	set "ClolorYellow_NT=[33m"
	set "ClolorBlue_NT=[34m"
	set "ClolorMagenta_NT=[35m"
	set "ClolorCyan_NT=[36m"
	set "ClolorWhite_NT=[37m"

	::	2 Normal Background
	set "ClolorBlack_NB=[40m"
	set "ClolorRed_NB=[41m"
	set "ClolorGreen_NB=[42m"
	set "ClolorYellow_NB=[43m"
	set "ClolorBlue_NB=[44m"
	set "ClolorMagenta_NB=[45m"
	set "ClolorCyan_NB=[46m"
	set "ClolorWhite_NB=[47m"

	::	3 Bright Text
	set "ClolorBlack_BT=[90m"
	set "ClolorRed_BT=[91m"
	set "ClolorGreen_BT=[92m"
	set "ClolorYellow_BT=[93m"
	set "ClolorBlue_BT=[94m"
	set "ClolorMagenta_BT=[95m"
	set "ClolorCyan_BT=[96m"
	set "ClolorWhite_BT=[97m"

	::	4 Bright Background
	set "ClolorBlack_BB=[100m"
	set "ClolorRed_BB=[101m"
	set "ClolorGreen_BB=[102m"
	set "ClolorYellow_BB=[103m"
	set "ClolorBlue_BB=[104m"
	set "ClolorMagenta_BB=[105m"
	set "ClolorCyan_BB=[106m"
	set "ClolorWhite_BB=[107m"

	::	0 DefaultClolor
	set "DefaultClolor=[0m"

	exit /b

:Checkdir
	rem ====----	https://stackoverflow.com/questions/18639663/how-to-pass-command-line-parameters-with-space-in-batch-file

	if exist "%*" (
		exit /b
	)
	if not exist "%*" (
		mkdir "%*"
	)
	if exist "%*" (
		echo %* Created
	)
	exit /b

:IsAdmin
	reg.exe query "HKU\S-1-5-19\Environment" >nul
	if not %ERRORLEVEL% EQU 0 (
		echo 必須「以系統管理員身分執行」
		echo You must have administrator rights to continue ...
		pause & exit
	)
	exit /b

:End
	%ComSpec% /%EndCmd%